//
//  MovieListViewController.m
//  FNer
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 刘志强. All rights reserved.
//

#import "MovieListViewController.h"
#import "MovieListCollectionViewCell.h"
#import "MovieDataViewController.h"
#import "UINavigationController+animation.h"
#import "VideoDetailViewController.h"

@interface MovieListViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@end

@implementation MovieListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.title = self.model.name;
    self.view.backgroundColor = [UIColor clearColor];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width , 150);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64) collectionViewLayout:flowLayout];
    self.myCollectionView.dataSource = self;
    self.myCollectionView.delegate = self;
    self.myCollectionView.backgroundColor = [UIColor clearColor];
    [self.myCollectionView registerClass:[MovieListCollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
    
    [self.view addSubview:self.myCollectionView];
    
    
    self.allDataArray = [NSMutableArray array];
    self.isUpLoading = YES;
     [self addHeaderRefresh];
    
    
}

- (void)addHeaderRefresh
{
    
    
    self.myCollectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isUpLoading =NO;
        //    self.array = [NSMutableArray array];
        
        
        [self AFNGetData:self.model.dataUrl];
        
        
    }];
    
       [self.myCollectionView.header beginRefreshing];
}

- (void)addFooterRefresh
{
    
    self.myCollectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.isUpLoading =YES;
       
        
        
    }];
}



-(void)AFNGetData:(NSString *)url
{
    
    NSString *urlEncode = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    if (self.isUpLoading == YES) {
        [self.allDataArray removeAllObjects];
    }
    
    [AFNTools getAFN:urlEncode andpara:nil andBlock:^(id result) {
        
        NSMutableArray *array = [result objectForKey:@"videoList"];
        
        for (NSMutableDictionary *dic in array) {
            
            MovieCoreData *model = [NSEntityDescription insertNewObjectForEntityForName:@"MovieCoreData" inManagedObjectContext:[[CoreDataManager shareManager] managedObjectContext]];
           // [model setValuesForKeysWithDictionary:dic];
            model.title = [dic objectForKey:@"title"];
            model.coverForFeed = [dic objectForKey:@"coverForFeed"];
             model.descriPtion = [dic objectForKey:@"description"];
            model.playUrl = [dic objectForKey:@"playUrl"];
            [self.allDataArray addObject:model];
        }
        [self.myCollectionView reloadData];
        [self.myCollectionView.header endRefreshing];
        [self.myCollectionView.footer endRefreshing];
    } andfailBlock:^(id fail) {
        [self.myCollectionView.header endRefreshing];
        [self.myCollectionView.footer endRefreshing];
        
    }];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.allDataArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *reuser = @"Cell";
    MovieCoreData *model = [self.allDataArray objectAtIndex:indexPath.row];
    MovieListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuser forIndexPath:indexPath];
    cell.model = model;
    
    return cell;
    
    
    
}







- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
   
    
    MovieCoreData *modell = [self.allDataArray objectAtIndex:indexPath.row];
    MovieDataViewController *movieDataVC = [[MovieDataViewController alloc] init];
    movieDataVC.model = modell;
    [SHAREDAPPDELE.myTB isHiddenTabBar:YES withViewController:movieDataVC];

    [self.navigationController pushViewController:movieDataVC animatedWithTransition:UIViewAnimationTransitionFlipFromLeft];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
